#include "common.h"

int minOperations(vector<int>& nums, int x)
{
	int n = nums.size();
	int sum = 0;
	for (int i = 0; i < n; i++)
	{
		sum += nums[i];
	}
	if (sum < x) return -1;
	int target = sum - x;
	int ans = n + 1;
	int left = 0;
	int right = 0;
	int temp = 0;
	while (right < n)
	{
		temp += nums[right];
		right++;
		while (temp > target && left <= right)
		{
			temp -= nums[left];
			left++;
		}
		if (temp == target)
			ans = min(ans, n - (right - left));
	}
	if (ans == n + 1) return -1;
	return ans;
}